/*******************************************************************************
* @copyright: Shenzhen Hangshun Chip Technology R&D Co., Ltd
* @filename:  main.c
* @brief:     Main program body
* @author:    AE Team
* @version:   V1.0.0/2025-07-16
*             1.Initial version
* @log:
*******************************************************************************/


/* Includes ------------------------------------------------------------------*/
#include "main.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint32_t TimingDelay = 0;
__IO uint32_t LsiFreq = 60000;
extern __IO uint16_t CaptureNumber;

/* Private function prototypes -----------------------------------------------*/
void OptionByteConfig_LSI_LP_CTL(void);
void Delay(__IO uint32_t nTime);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (KEIL_startup_hk32g003.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_hk32g003.c file
     */

  /* Initialize LED and Tamper Button mounted on the Eval board */
  HK_EVAL_LEDInit(LED1);
  HK_EVAL_LEDInit(LED2);
  HK_EVAL_PBInit(BUTTON_1, BUTTON_MODE_EXTI);

  /* Enable PWR clock */
  RCC_APBPeriph1ClockCmd(RCC_APBPeriph1_PWR, ENABLE);

  /* Setup SysTick Timer for 1msec interrupts */
  if(SysTick_Config(SystemCoreClock / 1000))
  {
    /* Capture error */
    while(1)
	{
	}
  }

  Delay(400);

  /* Check if the system has resumed from IWDG reset */
  if(RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET)
  {
    /* IWDGRST flag set */
    /* Turn on LED1 */
    HK_EVAL_LEDOn(LED1);

    /* Clear reset flags */
    RCC_ClearFlag();
  }
  else
  {
    /* IWDGRST flag is not set */

    /* Turn off LED1 */
    HK_EVAL_LEDOff(LED1);
  }

  /* Enalbe option byte LSI_LP_CTL  */
  OptionByteConfig_LSI_LP_CTL();

  /* IWDG timeout equal to 250 ms (the timeout may varies due to LSI frequency
     dispersion) */
  /* Enable write access to IWDG_PR and IWDG_RLR registers */
  IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);

  /* IWDG counter clock: LSI/32 */
  IWDG_SetPrescaler(IWDG_Prescaler_32);

  /* Set counter reload value to obtain 250ms IWDG TimeOut.
     Counter Reload Value = 1000ms/IWDG counter clock period
                          = 1000ms / (LSI/32)
                          = 1s / (LsiFreq/32)
                          = LsiFreq/(32 * 1)
                          = LsiFreq/32
   */
  IWDG_SetReload(LsiFreq / 32);

  /* Reload IWDG counter */
  IWDG_ReloadCounter();

  /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
  IWDG_Enable();

  while(1)
  {
    HK_EVAL_LEDOn(LED2);
    Delay(200);
    HK_EVAL_LEDOff(LED2);
    Delay(200);

    /* Enter stop mode */
    PWR_EnterStopMode(PWR_Regulator_LowPower, PWR_Entry_WFI);

    /* Toggle LED2 */
    HK_EVAL_LEDToggle(LED2);

    /* Insert 800 ms delay */
    Delay(400);

    /* Reload IWDG counter */
    IWDG_ReloadCounter();
  }
}

/**
  * @brief  Inserts a delay time.
  * @param  nTime: specifies the delay time length, in 10 ms.
  * @retval None
  */
void Delay(__IO uint32_t nTime)
{
  TimingDelay = nTime;

  while(TimingDelay != 0)
  {
	  
  }
}

/**
  * @brief  optionByte config.
  * @param  None.
  * @retval None.
  */
void OptionByteConfig_LSI_LP_CTL(void)
{
  uint8_t OB_UserReadBack = 0;
  uint8_t LSILP_ReadBack = 0;

  FLASH_Unlock();
  FLASH_OB_Unlock();
  OB_UserReadBack = FLASH_OB_GetUser();
  LSILP_ReadBack = *(uint32_t*)0x1FFFF014;

  if(((OB_UserReadBack & 0x2) == 0) || (LSILP_ReadBack & 0xFFFF) != 0x369C)
  {
    /* Erase option bytes */
    FLASH_OB_Erase();

    FLASH_OB_UserConfig(OB_IWDG_SW, OB_STOP_NoRST);

    FLASH_OB_ProgramData(0x1FFFF014, 0xFFFF369C);

  }

  FLASH_OB_Lock();
  FLASH_Lock();
}

/**
  * @brief  Decrements the TimingDelay variable.
  * @param  None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
  if(TimingDelay != 0x00)
  {
    TimingDelay--;
  }
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while(1)
  {
  }
}
#endif


